package com.linewell.demo;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;

/**
 * @Title: license-215
 * @description: 文件IO类。
 * @author: hyuhuang@linewell.com
 * @since:2018/8/29
 */
public class FileIO {
    @Test
    public void test(){
        try {
            InputStream io = new FileInputStream("d://123.zip");
            writeFileToDisk2(io,"e://1.zip");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取一行txt文件
     * @param name
     * @return
     */
    @Test
    public void toArrayByInputStreamReader1() {

        String name = "E:\\false.txt";
        // 使用ArrayList来存储每行读取到的字符串
        ArrayList<String> arrayList = new ArrayList<>();
        try {
            File file = new File(name);
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader bf = new BufferedReader(inputReader);
            // 按行读取字符串
            String str;
            while ((str = bf.readLine()) != null) {
                arrayList.add(str);
            }
            bf.close();
            inputReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对ArrayList中存储的字符串进行处理
        int length = arrayList.size();
        for (int i = 0; i < length; i++) {
            String[] s = arrayList.get(i).split(",");
            String licenseId = s[1];
            String deptId = s[2];
            String licenseAreaCode = s[3];
            String deptAreaCode = s[4];
            System.out.println(s);
        }
    }

    /**
     * 边读边写   此方法用于读取文本流，  zip流中不带空格，无法使用readline()
     */
//    @Test
    public void writeFileToDisk(InputStream inputStream,String path) {
        BufferedReader br = null;
        Writer writer = null;
        BufferedWriter bw = null;
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream);
            //读文件流
            br = new BufferedReader(inputStreamReader);

            //第二部分：准备从程序写到文件；创建写入对象writer
            writer = new FileWriter(new File(path));
            //创建缓冲流包装writer
            bw = new BufferedWriter(writer);

            String str = null;
            //用循环边读边写
            while ((str = br.readLine()) != null) {
                bw.write(str);
//                bw.newLine();
            }
            bw.flush(); //清空缓冲区
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



    /**
     * 边读边写 1       此方法通用。
     */
    public void writeFileToDisk2(InputStream inputStream,String path) {
        BufferedInputStream  bis = null;
        BufferedOutputStream bos = null;
        OutputStream outputStream = null;
        try {
            //读文件流
            bis = new BufferedInputStream(inputStream,8*1024);
            int bisLength=bis.available();
            int readLength=0;
            byte[] byteArray=new byte[4096];
            int tmp=0;
            outputStream = new FileOutputStream(path);
            bos = new BufferedOutputStream(outputStream);
            while((tmp=bis.read(byteArray))!=-1){
                bos.write(byteArray);
            }
            bos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 输入流转byte[]
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static byte[] inputStreamToByte(InputStream inputStream) throws IOException {
        String pk;
        Reader reader =  new InputStreamReader(inputStream);
        StringWriter writer = new StringWriter();

        char[] buffer = new char[4096];
        int amount;
        while ((amount = reader.read(buffer)) >= 0) {
            writer.write(buffer, 0, amount);
        }
        pk = writer.toString();
        inputStream.close();
        reader.close();
        writer.close();
        return pk.getBytes();
    }
}
